package com.ruoyi.notify.service.impl;


import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendBatchSmsResponse;
import com.rabbitmq.client.Channel;
import com.ruoyi.common.core.domain.NotifyLogVo;
import com.ruoyi.common.rabbitmq.constant.RabbitmqConstants;
import com.ruoyi.common.rabbitmq.domain.SmsNotifyTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;


@Service
public class SmsNotifyServiceImpl {

    @Autowired
    Client client;

//    @Autowired
//    RemoteNotifyLogService remoteNotifyLogService;

    @RabbitListener(queues = {RabbitmqConstants.SMS_NOTIFY_QUEUE})
    @RabbitHandler
    public void SMSSendBatch(SmsNotifyTemplate smsNotifyTemplate, Message message, Channel channel) throws IOException {
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);

        SendBatchSmsRequest request = new SendBatchSmsRequest();
        request.setTemplateParamJson(smsNotifyTemplate.getTemplateParamJson());
        request.setSignNameJson(smsNotifyTemplate.getSignNameJson());
        request.setTemplateCode(smsNotifyTemplate.getSignNameJson());
        request.setPhoneNumberJson(smsNotifyTemplate.getTemplateParamJson());


        SendBatchSmsResponse sendBatchSmsResponse = null;
        Integer flag = 1;
        boolean isAck = true;
        try {
            sendBatchSmsResponse = client.sendBatchSms(request);
        } catch (Exception e) {
            flag = 0;
            e.printStackTrace();
        }finally {
            String remark = null;

            if(sendBatchSmsResponse == null){
                isAck = false;
                remark = "网络异常或位置故障";
            }else{
                remark =sendBatchSmsResponse.getBody().getMessage();
            }

            List<NotifyLogVo>  vos = new LinkedList<>();
            List<Long> notifyUserIds = smsNotifyTemplate.getNotifyUserId();
            List<String> notifyUserNames = smsNotifyTemplate.getNotifyUserName();

            for(int i = 0;i < notifyUserIds.size(); i++){
                NotifyLogVo vo = new NotifyLogVo();

                vo.setNotifyTime(System.currentTimeMillis());
                vo.setNotifyUserId(notifyUserIds.get(i));
                vo.setNotifyUserName(notifyUserNames.get(i));
                vo.setNotifyType(1);
                vo.setRemark(remark);
                vo.setNotifyStatus(flag);
                vo.setContent(smsNotifyTemplate.getTemplateParamJson());
                vos.add(vo);
            }



//            if(isAck){
//                channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//            }
//            try{
//                remoteNotifyLogService.addNotify(vos);
//            }catch (Exception e){
//                e.printStackTrace();
//            }
        }

    }






}
